<?php

namespace app\stores\controller\admin;

use app\BaseController;

use app\beautiful\model\logs\GoodsViewsModel;

use app\beautiful\model\order\GoodsOrderItemModel;

use app\beautiful\model\order\GoodsOrderModel;

use app\beautiful\model\order\StoreOrderModel;
use app\stores\model\order\BuyOrderModel;


class Statistics extends BaseController{

    public function getMallStatisticsCount()

    {

        $today_base_order_data=GoodsOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[2,3,4,8,9,10,11,13]],

        ])->whereDay('add_time')->field('count(*) as count,sum(need_pay) as order_money')->find();



        $today_buy_order_data=BuyOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[1,2]],

        ])->whereDay('add_time')->field('count(*) as count,sum(pay_money) as order_money')->find();



        $total_base_order_data=GoodsOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[2,3,4,8,9,10,11,13]],

        ])->field('count(*) as count,sum(need_pay) as order_money')->find();



        $total_buy_order_data=BuyOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[1,2]],

        ])->field('count(*) as count,sum(pay_money) as order_money')->find();



        $today_order_num=$today_base_order_data->count + $today_buy_order_data->count;

        $today_order_money=round((($today_base_order_data->order_money === null ? 0 : $today_base_order_data->order_money *100) +

            ($today_buy_order_data->order_money === null ? 0 : $today_buy_order_data->order_money *100))/100,2);



        $total_order_num=$total_base_order_data->count + $total_buy_order_data->count;

        $total_order_money=round((($total_base_order_data->order_money === null ? 0 : $total_base_order_data->order_money * 100) +

            ($total_buy_order_data->order_money === null ? 0 : $total_buy_order_data->order_money * 100))/100,2);



        $datas=[

            'today_order_num'=>$today_order_num,

            'today_order_money'=>$today_order_money,

            'total_order_num'=>$total_order_num,

            'total_order_money'=>$total_order_money,

        ];

        $return['datas']=$datas;



        return $this->success($return);

    }



    public function getMallStatisticsTakeCountRanking()

    {

        $time_type=$this->request->param('time_type/d',1);

        if(!in_array($time_type,[1,2])) $time_type=1;

        $order_where=[

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','>',1],

        ];

        $order_model= new GoodsOrderModel();

        $order_model=$order_model->where($order_where);

        if($time_type==1) $order_model=$order_model->whereMonth('add_time');

        if($time_type==2) $order_model=$order_model->whereYear('add_time');

        $sql=$order_model->field('order_id')->buildSql();



        

        $total=GoodsOrderItemModel::alias('a') ->join([$sql=> 'w'], 'a.order_id = w.order_id')->where([

            ['a.shop_id','=',$this->request->shopId]

        ])->field('sum(a.goods_num) as take_count,a.goods_id')->group('a.goods_id')->count();



        $list=GoodsOrderItemModel::alias('a') ->join([$sql=> 'w'], 'a.order_id = w.order_id')->with(['goods'])->where([

            ['a.shop_id','=',$this->request->shopId]

        ])->order(['take_count'=>'desc'])->field('sum(a.goods_num) as take_count,a.goods_id')->group('a.goods_id')

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->toArray();

        foreach ($list as $k =>$v){

            $list[$k]['rank']=$this->request->limit * ($this->request->page - 1) + $k + 1;

        }



        $return=[

            'total'=>$total,

            'list'=>$list

        ];



        return $this->success($return);

    }



    public function getMallStatisticsViewsRanking()

    {

        $time_type=$this->request->param('time_type/d',1);

        if(!in_array($time_type,[1,2])) $time_type=1;

        $goods_views_model = new GoodsViewsModel();

        $where=[

            ['shop_id','=',$this->request->shopId],

        ];

        $goods_views_model=$goods_views_model->where($where);

        if($time_type==1) $goods_views_model=$goods_views_model->whereMonth('add_time');

        if($time_type==2) $goods_views_model=$goods_views_model->whereYear('add_time');

        $total=$goods_views_model->group('goods_id')->field('goods_id,count(goods_id) as view_count')->count();



        $list =$goods_views_model->with(['goods'])

            ->order(['view_count'=>'desc'])

            ->group('goods_id')

            ->field('goods_id,count(goods_id) as view_count')

            ->select()

            ->toArray();



        foreach ($list as $k =>$v){

            $list[$k]['rank']=$this->request->limit * ($this->request->page - 1) + $k + 1;

        }



        $return=[

            'total'=>$total,

            'list'=>$list

        ];



        return $this->success($return);

    }



    public function getMallStatisticsTrend()

    {

        $days=[];

        for($template=14;$template>0;$template--){

            $days[]=date('Y-m-d',strtotime('-'.$template.' days'));

        }

        $days[]=date("Y-m-d");

        $base_order_data=GoodsOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[2,3,4,8,9,10,11,13]],

        ])->whereTime('add_time','-15 days')

            ->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")

            ->field("count(*) as count,sum(need_pay) as order_money,FROM_UNIXTIME(add_time,'%Y-%m-%d') as day")->select()->toArray();



        $buy_order_data=BuyOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[1,2]],

        ])->whereTime('add_time','-15 days')->group("FROM_UNIXTIME(add_time,'%Y-%m-%d')")

            ->field("count(*) as count,sum(pay_money) as order_money,FROM_UNIXTIME(add_time,'%Y-%m-%d') as day")->select()->toArray();



        $order_data=[];

        foreach ($days as $v){

            $item=[];

            $item['日期']=$v;

            $count = $total_price = 0;

            foreach ($base_order_data as $key => $val){

                if($v==$val['day']){

                    $count += $val['count'] ?? 0;

                    $total_price += $val['order_money'] ?? 0;

                }

            }

            foreach ($buy_order_data as $key => $val){

                if($v==$val['day']){

                    $count += $val['count'] ?? 0;

                    $total_price += $val['order_money'] ?? 0;

                }

            }

            $item['订单数']=$count;

            $item['订单金额']=$total_price;

            $order_data[]=$item;

        }

        $yesterday_order_data=GoodsOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[2,3,4,8,9,10,11,13]],

        ])->whereDay('add_time','yesterday')->field('count(*) as count,sum(need_pay) as order_money')->find();



        $yesterday_buy_order_data=BuyOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[1,2]],

        ])->whereDay('add_time','yesterday')->field('count(*) as count,sum(pay_money) as order_money')->find();



        $total_order_data=GoodsOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[2,3,4,8,9,10,11,13]],

        ])->field('count(*) as count,sum(need_pay) as order_money')->find();



        $total_buy_order_data=BuyOrderModel::where([

            ['shop_id','=',$this->request->shopId],

            ['is_paid','=',1],

            ['status','In',[1,2]],

        ])->field('count(*) as count,sum(pay_money) as order_money')->find();



        $yesterday_order_num=$yesterday_order_data->count + $yesterday_buy_order_data->count;

        $yesterday_order_money=round((($yesterday_order_data->order_money === null ? 0 : $yesterday_order_data->order_money *100) +

            ($yesterday_buy_order_data->order_money === null ? 0 : $yesterday_buy_order_data->order_money * 100))/100,2);



        $total_order_num=$total_order_data->count + $total_buy_order_data->count;

        $total_order_money=round((($total_order_data->order_money === null ? 0 : $total_order_data->order_money * 100) +

            ($total_buy_order_data->order_money === null ? 0 : $total_buy_order_data->order_money * 100))/100,2);



        $order_total_data=[

            'yesterday_order_num'=>$yesterday_order_num,

            'yesterday_order_money'=>$yesterday_order_money,

            'total_order_num'=>$total_order_num,

            'total_order_money'=>$total_order_money,

        ];



        $return=[

            'order_data'=>$order_data,

            'order_total_data'=>$order_total_data

        ];

        return $this->success($return);

    }

}

